package com.uum.blog.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uum.blog.pojo.entity.Blog;
import com.uum.blog.pojo.form.BlogForm;
import com.uum.blog.pojo.query.BlogPageQuery;
import com.uum.blog.pojo.vo.BlogVO;
import com.uum.blog.service.BlogService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@Api(tags = "博文接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/blog")
public class BlogController {

    private final BlogService blogService;

    @Operation(description = "查询博文列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation(value = "博文列表分页")
    @GetMapping("page")
    public Result blogPage(BlogPageQuery pageQuery) {
        IPage<BlogVO> iPage = blogService.page(pageQuery);
        return Result.success(iPage);
    }

    @ApiOperation(value = "获取搜索建议")
    @GetMapping("listSuggest/{title}")
    public Result listSuggest(@PathVariable String title){
        List<Map<String,String>> suggestList = blogService.listSuggest(title);
        return Result.success(suggestList);
    }

    @ApiOperation(value = "博文详情")
    @GetMapping("detail/{id}")
    public Result blogDetail(@PathVariable Long id) {
        BlogVO blog = blogService.detail(id);
        return Result.success(blog);
    }

    @Operation(description = "修改博文",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改博文")
    @PutMapping("update")
    public Result updateBlog(@RequestBody BlogForm blog) {
        Boolean result = blogService.updateBlog(blog);
        return Result.judge(result);
    }

    @Operation(description = "设置博文",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "设置博文")
    @PutMapping("setting")
    public Result settingBlog(@RequestBody Blog blog) {
        boolean result = blogService.settingBlog(blog);
        return Result.judge(result);
    }

    @Operation(description = "添加博文",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加博文")
    @PostMapping("add")
    public Result addBlog(@RequestBody BlogForm blog) {
        Boolean result = blogService.addBlog(blog);
        return Result.judge(result);
    }

    @Operation(description = "删除博文",businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除博文")
    @DeleteMapping("delete/{ids}")
    public Result deleteBlog(@PathVariable String ids) {
        Boolean result = blogService.delete(ids);
        return Result.judge(result);
    }

    @Operation(description = "导入文件",businessType = BusinessType.IMPORT)
    @ApiOperation("导入markdown格式的文件")
    @PostMapping("uploadMarkdownFile")
    public Result uploadMarkdownFile(@RequestBody MultipartFile fileData) {
        String content = blogService.uploadMarkdownFile(fileData);
        String title = fileData.getOriginalFilename().substring(0, fileData.getOriginalFilename().indexOf("."));
        return Result.success().add("content", content).add("title",title);
    }

    @ApiOperation(value = "修改博文作者名字")
    @PutMapping("updateAuthorName")
    public Result updateAuthorName(@RequestParam("userId") Long userId,@RequestParam("nickname") String nickname) {
        Boolean result = blogService.updateAuthorName(userId,nickname);
        return Result.judge(result);
    }
}

